<!doctype html>
<html lang="en">
    <meta http-equiv="Content-Type" content="text/html;charset=utf8">
    <style type="text/css">
        .wrap {
            background-color: aqua;
            width: 400px;
            height: 200px;
            margin: 0 auto;
            text-align: center;
            margin-top: 200px;
        }

        .wrap select {
            width: 120px;
            height: 30px;
        }

        #my {
            width: 400px;
            height: 200px;
            margin: 0 auto;
        }
    </style>
    <script type="text/javascript" src="jquery-3.2.1.min.js"></script>
    <body>
        <h2>Hello World!你好</h2>
        <div class="wrap">
            <select id="province"></select>
            <select id="city"></select>
        </div>
        <div id="my">
            <input style="border-radius: 18px;background-color: antiquewhite" type="text" id="user" placeholder="输入内容">
            <div id="info"></div>
        </div>


        <script type="text/javascript">
            /* 设置时间间隔 */
            /*var time = setInterval("search();",100);*/

            $("#user").keyup(function () {
                search();
            })

            function search() {
                var key = $("#user").val();
                $.ajax({
                    url: "/userServlet?key=" + key,
                    dataType: "json"
                }).done(function (data) {
                    $("#info").empty();
                    for (var i in data) {
                        $("#info").append($("<div style='cursor: pointer' id='" + data[i].id + "'>" + data[i].name + "</div>"));
                    }

                    $("#info").children("div").on("click", function () {
                        var i = $(this).attr("id");
                        var t = $("#" + i).text();
                        $("#user").val(t);
                        $("#info").empty();
                    });
                });
            }
        </script>

        <script type="text/javascript">

            function getCityData() {
                $("#city").empty();
                var pid = $("#province").val();
                $.ajax({
                    url: "/myCity?pid=" + pid,
                    dataType: "json"
                }).done(function (data) {
                    for (var i in data) {
                        var provinceData = data[i];
                        $("#city").append($("<option value='" + data[i].id + "'>" + data[i].name + "</option>"))
                    }
                });
            }

            $.ajax({
                url: "/myProvince",
                dataType: "json"
            }).done(function (data) {
                for (var i in data) {
                    var provinceData = data[i];
                    $("#province").append($("<option value='" + data[i].id + "'>" + data[i].name + "</option>"))
                }
                getCityData();
            });

            $("#province").change(function () {
                getCityData();
            })
        </script>
    </body>
</html>
